package com.xyb.controller;

import com.xyb.entity.Chapter;
import com.xyb.service.ChapterService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("chapter")
public class ChapterController {
    @Resource
    private ChapterService chapterService;

    @GetMapping("chapter/{courseId}")
    public List<Chapter> selectChapterListByCourseId(@PathVariable("courseId") Long courseId){
        List<Chapter> list = chapterService.selectChapterListByCourseId(courseId);
        return list;
    }

    @GetMapping("chapter/course-detail/{courseId}")
    public List<Chapter> selectChapterListAndCourseDetailsByCourseId(@PathVariable("courseId") Long courseId){
        List<Chapter> list = chapterService.selectChapterListAndCourseDetailsByCourseId(courseId);
        return list;
    }

    @PostMapping("chapter/{courseId}")
    public String insertChapterByCourseId(@PathVariable("courseId") Long courseId,@RequestBody @Validated(Chapter.insert.class) Chapter chapter){
        chapter.setCourseId(courseId);
        return chapterService.insertChapterByCourseId(chapter);
    }

    @PutMapping("chapter/{chapterId}")
    public String updateChapterByChapterId(@PathVariable("chapterId") Long chapterId,@RequestBody @Validated(Chapter.update.class) Chapter chapter){
        chapter.setChapterId(chapterId);
        return chapterService.updateChapterByChapterId(chapter);
    }

    @DeleteMapping("chapter/{chapterId}")
    public String deleteChapterByChapterId(@PathVariable("chapterId") Long chapterId){
        return chapterService.deleteChapterByChapterId(chapterId);
    }

    @PutMapping("chapter/order")
    public String updateChapterShowOrder(@RequestBody Long[] chapterIds){
        return chapterService.updateChapterShowOrder(chapterIds);
    }
}
